package cn.edu.guet.controller;

import cn.edu.guet.bean.editor.Badge;
import cn.edu.guet.service.badge.IBadgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping
public class badgeController {

    @Autowired
    private IBadgeService iBadgeService;

    @GetMapping("test.do")
    public String test() {
        return "test";
    }

    @GetMapping("badgeSet.do")
    public String badgeSetView(Model model) {
//        return "html/badge/badgeSet";
        model.addAttribute("page","html/badge/badgeSet");
        return "forward:/main";
    }

    //局部刷新添加徽章页面
    @GetMapping("badgeAdd.do")
    public String toAddBadge() {
        return "html/badge/badgeADD";
    }

    //局部刷新修改徽章页面,显示所有勋章信息
    @GetMapping("badgeUpdate.do")
    public String toUpdateBadge(HttpServletRequest request) {
        List<Badge> badgeList = iBadgeService.getAllBadge();
        request.setAttribute("badgeList", badgeList);
        return "html/badge/badgeUpdate";
    }

    //添加徽章
    @PostMapping("addBadge.do")
    @ResponseBody
    public int addBadge(String bname, String bmes, String bprice, MultipartFile file) {
        String fileType = file.getContentType();
        if (fileType.equals("image/jpeg") || fileType.equals("image/png") || fileType.equals("image/jpeg")) {
            String fileName = file.getOriginalFilename();
            //获取文件后缀名
            int index = fileName.lastIndexOf(".");
            String suffixName;
            if (index > 0) {
                suffixName = fileName.substring(fileName.lastIndexOf("."));
            } else {
                suffixName = ".png";
            }
            fileName = UUID.randomUUID() + suffixName;
            System.out.println("fileName" + fileName);

            //获取项目路径
            Resource resource = new ClassPathResource("");
            try {
                String projectPath = resource.getFile().getAbsolutePath();
                System.out.println("projectPath" + projectPath);
                String relativePath = "../../images/badge/" + fileName;
                // 将img文件存入本地
                String path = projectPath + "\\static\\" + "images\\badge\\" + fileName;
                System.out.println("path" + path);

                File dest = new File(path);

                //判断文件父目录是否存在

                if (!dest.getParentFile().exists()) {
                    dest.getParentFile().mkdir();
                }

                //保存文件
                try {
                    file.transferTo(dest);
                } catch (IllegalStateException | IOException e) {
                    e.printStackTrace();
                }

                Badge badge = new Badge();
                badge.setBname(bname);
                badge.setBpath(relativePath);
                badge.setBmes(bmes);
                badge.setBprice(bprice);
                iBadgeService.addBadge(badge);
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return 1;
    }

    //修改徽章
    @PostMapping("updateBadge.do")
    @ResponseBody
    public int updateBadge(int bid, String bname, String bmes, String bprice) {
        Badge badge = new Badge();
        badge.setBid(bid);
        badge.setBname(bname);
        badge.setBmes(bmes);
        badge.setBprice(bprice);
        iBadgeService.updateBadge(badge);
        return 1;
    }
    //删除徽章
    @PostMapping("deleteBadge.do")
    @ResponseBody
    public int updateBadge(int bid,String bpath) throws IOException {
        String pathName=bpath.substring(bpath.indexOf("badge"),bpath.length());
        //获取target路径
        Resource resource = new ClassPathResource("");
        String projectPath = resource.getFile().getAbsolutePath();//target绝对路径

        String onloadPath = projectPath+"\\static\\" + "images\\"+pathName;
        File picture = new File(onloadPath);
        picture.delete();
        iBadgeService.deleteBadge(bid);
        return 1;
    }
}
